/*
 * $Id$
 */
package corejs.parser;

import corejs.stmt.Statement;

import static corejs.JsTokenType.*;
import corejs.expr.Expression;
import corejs.stmt.If;

/**
 *
 * @author myang
 */
public class IfParser implements StatementParser {

   @Override
   public If statement(final PrattParser parser) {
      final If iff = new If();

      parser.nextToken(LEFT_PAREN);
      iff.test(parser.expr());
      parser.nextToken(RIGHT_PAREN);

      if (parser.match(LEFT_BRACE)) {
         iff.iff(parser.block(RIGHT_BRACE));
      } 
      else {
         iff.iff(parser.statement());
      }

      if (parser.match(ELSE)) {
         if (parser.match(IF)) {
            iff.els(statement(parser));
         } 
         else {
            if (parser.match(LEFT_BRACE)) {
               iff.els(parser.block(RIGHT_BRACE));
            } 
            else {
               iff.els(parser.statement());
            }
         }
      }
      return iff;
   }
}
